﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace Orktane.Behaviors
{
    public class UpdateCursorAction
        : TriggerAction<TextBox>
    {
        public static readonly DependencyProperty CursorPositionProperty =
            DependencyProperty.Register("CursorPosition", typeof(CursorPosition), typeof(UpdateCursorAction),
            new PropertyMetadata(CursorPosition.End));

#region Properties

        public CursorPosition CursorPosition
        {
            get { return (CursorPosition)GetValue(CursorPositionProperty); }
            set { SetValue(CursorPositionProperty, value); }
        }

#endregion

#region Overrides

        protected override void Invoke(object parameter)
        {
            if (this.AssociatedObject == null || string.IsNullOrEmpty(this.AssociatedObject.Text)) return;
            this.AssociatedObject.SelectionLength = 0;
            this.AssociatedObject.SelectionStart = this.CursorPosition == Behaviors.CursorPosition.Start ? 0 : this.AssociatedObject.Text.Length;
        }

#endregion

    }    
}
